
package com.movecar.widget;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.movecar.application.MainApplication;

/**
 * 主页用自定义ImageView
 * 
 * @author
 * 
 */
public class MoveCarImageview extends ImageView {

	private Context mContext;
	public final String TAG = MoveCarImageview.class.getSimpleName();

	public MoveCarImageview(Context context, AttributeSet attrs) {

		super(context, attrs);
		mContext = context;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		imageSize();
	}

	/**
	 * 
	 * 
	 */
	public void imageSize() {

		float width = MainApplication.screenWidthDip;
		// 读取数据
		SharedPreferences preferences = mContext.getSharedPreferences("img", 0);
		int widthx = Integer.parseInt(preferences.getString("width", "").equals("") ? "0" : preferences.getString(
				"width", ""));
		int hight = Integer.parseInt(preferences.getString("hight", "").equals("") ? "0" : preferences.getString(
				"hight", ""));
		LayoutParams para = this.getLayoutParams();
		para.width = (int) (width);
		if (widthx != 0) {
			double n =  (double) widthx /(double)  hight;
			para.height = (int) ((width - 20) / n);
		} else {
			para.height = 0;
		}

		// para.height = 50;
		this.setLayoutParams(para);
	}
}